package org.jmpo;

public class SevenDivisibilityRule {
    public SevenDivisibilityRule(CharSequence seq) {
        this.seq = seq;
        if (0 == seq.length()) throw new IllegalArgumentException("An empty sequence");
    }

    public int process() {
        int currentReminder = 0;
        for (int idx = 0; idx != seq.length(); ++idx) {
            var r = currentReminder * 2 + translateSymbol(seq, idx);
            currentReminder = r >= 7 ? (r - 7) : r;
        }
        return currentReminder;
    }

    private int translateSymbol(CharSequence seq, int idx) {
        return switch(seq.charAt(idx)) {
            case '0' -> 0;
            case '1' -> 1;
            default  -> throw new IllegalArgumentException(String.format("Unexpected symbol %c at pos %d", seq.charAt(idx), idx));
        };
    }

    private final CharSequence seq;
}
